﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sispe.Entities;
using Sispe.BussinesLogic;
using Microsoft.Practices.Unity;

namespace Sispe.UI
{
    public partial class FrmMttoEstudio : Form
    {
        [Dependency]
        public IEstudioBL estudioBL { get; set; }

        [Dependency]
        public IGradoBL gradoBL { get; set; }
        public FrmMttoEstudio()
        {
            InitializeComponent();
            this.BuildUp();
        }

        private void btnTipoSocio_Click(object sender, EventArgs e)
        {
            FrmGrado frm = new FrmGrado();
            frm.Extend();
            frm.ShowDialog();

            if (frm.currentGrado != null)
            {
                var nuevoGrado = gradoBL.GetFromGradosById(frm.currentGrado.IdGrado);
                var estudio = eSTUDIOBindingSource.Current as ESTUDIO;
                estudio.GRADO = nuevoGrado;
                estudio.IdGrado = nuevoGrado.IdGrado;
                eSTUDIOBindingSource.ResetBindings(true);
            }
        }
        bool isNew;

        public void NuevoEstudio()
        {
            isNew = true;
            eSTUDIOBindingSource.AddNew();
        }

        public ESTUDIO _estudio { get; set; }

        public void EditarEstudio(int IdEstudios)
        {
            isNew = false;
            var estudio = estudioBL.GetFromEstudioById(IdEstudios);
            eSTUDIOBindingSource.DataSource = estudio;
            cbDescripcion.Text = estudio.Descripcion;
        }
        private void razonSocialTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void rucTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            var estudio = eSTUDIOBindingSource.Current as ESTUDIO;

            var msg = "ERROR";

            if (estudio != null)
            {
                if (isNew == true)
                {
                    try
                    {
                        estudio.Descripcion = cbDescripcion.Text.ToUpper();
                        estudioBL.AddEstudios(estudio);
                        MessageBox.Show("Registro Correcto", "Registro",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                if (isNew == false)
                {
                    try
                    {
                        estudio.Descripcion = cbDescripcion.Text.ToUpper();
                        estudioBL.UpdateEstudios(estudio);

                        MessageBox.Show("Modificación Correcta", "Mensaje",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void codigoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsNumber(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void descripcionTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var coma = Convert.ToString(",");
            var punto = Convert.ToString(".");
            var parentesis = Convert.ToString("()");
            var puntoComa = Convert.ToString(";");
            var slash = Convert.ToString("/");

            string cadena = coma + punto + parentesis + puntoComa + slash + (char)8;

            if (Char.IsLetter(e.KeyChar) || cadena.Contains(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void FrmMttoEstudio_Load(object sender, EventArgs e)
        {
            //eSTUDIOBindingSource.AddNew();
        }
    }
}
